/*
 * Copyright 2018 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.codegen.utils;

import org.apache.commons.lang3.StringUtils;

import com.lap.codegen.utils.CodeEnum.JavaType;

/**
 * <pre>
 * 数据库类型转换到java类型
 * </pre>
 * 
 * @author 劳水生
 * @version 1.0.0
 */
public final class TypeUtils {
	private TypeUtils() {
	}

	/**
	 * <pre>
	 * 数据库类型转换到java类型，目前支持的类型大致：
	 * bigint == Long
	 * int =Integer
	 * decimal() = Double
	 * varchar() = String
	 * datetime = Date;
	 * 
	 * </pre>
	 * 
	 * @param para
	 * @return
	 */
	public static String getJavaType(String para) {
		if (StringUtils.isNotBlank(para) && para.indexOf('(') != -1) {
			para = para.substring(0, para.indexOf('(')).toLowerCase();
		}
		switch (para) {
		case "tinyint":
		case "smallint":
		case "int":
			return JavaType.INT.getValue();
		case "datetime":
		case "timestamp":
		case "date":
			return JavaType.DATE.getValue();
		case "bigint":
			return JavaType.LONG.getValue();
		case "decimal":
			return JavaType.DOUBLE.getValue();
		default:
			return JavaType.STRING.getValue();
		}
	}

	/**
	 * <pre>
	 * 获取数据库的字段数据类型
	 * </pre>
	 * 
	 * @param para
	 * @return
	 */
	public static String getDbType(String para) {
		if (StringUtils.isNotBlank(para) && para.indexOf('(') != -1) {
			para = para.substring(0, para.indexOf('('));
		}
		return para;
	}

	/**
	 * 获取字段长度的字符串
	 * 
	 * @param para
	 * @return
	 */
	public static String getLength(String para) {
		if (StringUtils.isNotBlank(para) && para.contains("(")) {
			return para.substring(para.indexOf('(') + 1, para.indexOf(')'));
		}
		return "";
	}

}
